package com.yaodian100.mall.help.web;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.crm.cs.service.CsService;
import com.yaodian100.ec.member.entity.ImageValid;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.mall.member.util.UserData;

public class AdviceAction extends ActionSupport implements Preparable {
	private static final long serialVersionUID = 8619850489941179116L;

	private String redirUrl;
	
	private String class2;
	
	private String memberId;
	private String user;
	private String email;
	private String cellPhone;
	private String phoneArea;
	private String phone;
	private String phoneExt;
	private String content;
	private List<File> attach;
	private List<String> attachFileName;
	private List<String> attachContentType;
	private String validValue;
	
	private boolean validCaptcha = true;
	private List questionTypes;
	
	@Autowired
	private MemberService memberService;
	@Autowired 
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private CsService csService;
	
	public String main() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (null != userData && null != userData.getMemberId()) {
			Member member = this.memberService.getMemberByLoginId(userData.getLoginId());
			this.memberId = member.getMemberId();
			this.user = member.getLoginId();
			this.email = member.getEmail();
			this.cellPhone = member.getMobile();
		}
		return "input";
	}
	
	public String save() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (!checkCode(userData.getImageValid(), validValue)) {
		    validCaptcha = false;
		    return "input";
		}
		this.csService.createCommonQuestion(email, (userData!=null?userData.getMemberId():null), null, content, phoneArea, phone, phoneExt, null, cellPhone, class2, attach, attachFileName);
		validCaptcha = true;
		return "success";
	}
		
	private boolean checkCode(String imageId, String inputCode) {
		ImageValid imageValid = new ImageValid();
		imageValid.setId(imageId);
		imageValid.setValidData(inputCode);
		try {
			memberService.isImageValid(imageValid);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	public String getRedirUrl() {
		return redirUrl;
	}
	public void setRedirUrl(String redirUrl) {
		this.redirUrl = redirUrl;
	}
	public String getClass2() {
		return class2;
	}
	public void setClass2(String class2) {
		this.class2 = class2;
	}
	public String getMemberId() {
		return memberId;
	}
	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCellPhone() {
		return cellPhone;
	}
	public void setCellPhone(String cellPhone) {
		this.cellPhone = cellPhone;
	}
	public String getPhoneArea() {
		return phoneArea;
	}
	public void setPhoneArea(String phoneArea) {
		this.phoneArea = phoneArea;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPhoneExt() {
		return phoneExt;
	}
	public void setPhoneExt(String phoneExt) {
		this.phoneExt = phoneExt;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public List<File> getAttach() {
		return attach;
	}
	public void setAttach(List<File> attach) {
		this.attach = attach;
	}
	public List<String> getAttachFileName() {
		return attachFileName;
	}
	public void setAttachFileName(List<String> attachFileName) {
		this.attachFileName = attachFileName;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public MemberService getMemberService() {
		return memberService;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public String getValidValue() {
		return validValue;
	}

	public void setValidValue(String validValue) {
		this.validValue = validValue;
	}

	public boolean isValidCaptcha() {
		return validCaptcha;
	}

	public void setValidCaptcha(boolean validCaptcha) {
		this.validCaptcha = validCaptcha;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List getQuestionTypes() {
		return questionTypes;
	}

	public void setQuestionTypes(List questionTypes) {
		this.questionTypes = questionTypes;
	}

	public List<String> getAttachContentType() {
		return attachContentType;
	}

	public void setAttachContentType(List<String> attachContentType) {
		this.attachContentType = attachContentType;
	}

	public CsService getCsService() {
		return csService;
	}

	public void setCsService(CsService csService) {
		this.csService = csService;
	}

	@Override
	public void prepare() throws Exception {
		String sql = "SELECT ID,NAME FROM CRM_CS_COMMONQUESTION_TYPE WHERE PARENT_ID IS NOT NULL AND SHOWONQAA = 1 CONNECT BY PRIOR ID = PARENT_ID START WITH NAME = '网站服务建议' AND PARENT_ID IS NULL ORDER BY SORTORDER,ID";
		questionTypes = this.jdbcTemplate.queryForList(sql);
		
	}
}
